package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainingListAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainingListBean;
import com.shixi.shixitraining.shixitraining.util.TitleUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.List;

import retrofit2.Call;

public class HomeGrideTrainingActivity extends BaseActivity implements View.OnClickListener {


    private int DISTANCE_WHEN_TO_SELECTED = 40;
    private int distanceY = 0;
    private View homeTitleBarBgView;
    private Context activity;
    private FrameLayout mToolBar;
    private ImageView mBackImg;
    private TextView mTrainingTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_gride_training);
        initTitle();
        initBaseData();
    }

    private void initBaseData() {

        HttpUtil.getInstance().getApiInterface().trainingList("http://api.shixigroup.com/training/index").enqueue(new CallbackImple<TrainingListBean>() {
            @Override
            public void onError(Call<TrainingListBean> call, Throwable t) {
                ToastUtil.showShortToast(HomeGrideTrainingActivity.this,t.getMessage());
                Log.e("AAA",t.getMessage());
            }
            @Override
            public void onSuccess(Call<TrainingListBean> call, TrainingListBean response) {
                if (response.getStatus() == 200) {
                    if (response != null) {
                        if (response.getData() != null) {
                            final List<TrainingListBean.DataBean.ListsBean> lists = response.getData().getLists();
                            List<TrainingListBean.DataBean.CountryBean> country = response.getData().getCountry();
                            List<TrainingListBean.DataBean.SortBean> sort = response.getData().getSort();
                            List<TrainingListBean.DataBean.TypeBean> type = response.getData().getType();
                            RecyclerView mHomeRcView = (RecyclerView) findViewById(R.id.home_training_rcview);
                            GridLayoutManager manager=new GridLayoutManager(HomeGrideTrainingActivity.this,2);
                            manager.setOrientation(LinearLayoutManager.VERTICAL);
                            mHomeRcView.setLayoutManager(manager);
                            TrainingListAdapter adapter = new TrainingListAdapter(HomeGrideTrainingActivity.this, lists, Glide.with(HomeGrideTrainingActivity.this));
                            mHomeRcView.setAdapter(adapter);
                            mHomeRcView.addOnItemTouchListener(new RecycleItemClickListener(HomeGrideTrainingActivity.this) {
                                @Override
                                public void itemClickListener(View view, int position) {
                                    TrainingListBean.DataBean.ListsBean listsBean = lists.get(position);
                                    String id = listsBean.getId();
                                    Intent intent=new Intent(HomeGrideTrainingActivity.this, TrainingDetailActivity.class);
                                    Bundle bundle=new Bundle();
                                    bundle.putString("id",id);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                } else {
                    ToastUtil.showShortToast(HomeGrideTrainingActivity.this,response.getMsg());
                }

            }
        });
    }

    private void initTitle() {

        TitleUtil titleUtil = new TitleUtil(findViewById(R.id.top_bar_layout));
        titleUtil.initTitle(4, R.id.top_bar_center_tv, R.id.top_bar_left_img, 0, R.drawable.icon_back,
                0, "实训项目", "", "");
        ImageView mBackImg = (ImageView) findViewById(R.id.top_bar_left_img);
        mBackImg.setOnClickListener(this);
        View viewById = findViewById(R.id.lin_collection_training);
    }

    private void initReView() {
//        mHomeRcView.setOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                distanceY += dy;
//                if (distanceY > DensityUtil.dp2px(activity, 20)) {
//
//                    homeTitleBarBgView.setBackgroundColor(getResources().getColor(R.color.basepresscolor));
//                    if (Build.VERSION.SDK_INT > 21) {
//                        mBackImg.setVisibility(View.VISIBLE);
//                        mTrainingTitle.setVisibility(View.VISIBLE);
//                        homeTitleBarBgView.setAlpha(distanceY * 1.0f / DensityUtil.dp2px(activity, 100));
//                    }
//                    else {
//                        DISTANCE_WHEN_TO_SELECTED = 20;
//                    }
//                }
//                else {
//                    mBackImg.setVisibility(View.GONE);
//                    mTrainingTitle.setVisibility(View.GONE);
//                    homeTitleBarBgView.setBackgroundColor(0x0000000);
//                }
//
//            }
//        });

//        mBackImg.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_bar_left_img:
                this.finish();
                break;
        }
    }
}
